package com.buss.FrontController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.buss.article.entity.BnsArticleEntity;
import com.buss.articlegroup.entity.ArticleGroupInfoEntity;
import com.buss.articlegroup.service.ArticleGroupInfoServiceI;
import com.buss.theme.entity.BnsThemeEntity;

/**
 * @Title: Controller
 * @Description: 前台版块信息控制层
 * @author hlg
 * @date 2016-09-02
 * @version V1.0
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/frontGroupInfoController")
public class FrontGroupInfoController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FrontGroupInfoController.class);

	@Autowired
	private ArticleGroupInfoServiceI articleGroupInfoService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private CommonService commonService;

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 版块详情信息
	 * TODO 全部主题 投票 悬赏 辩论 | 最新 热门 热帖 精华等的查询
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "groupInfo")
	public ModelAndView groupInfo(HttpServletRequest request) {
		HttpSession session = request.getSession();
		// String mbId = (String) session.getAttribute("mbId");
		String groupId = request.getParameter("groupId");
		String themeId = request.getParameter("themeId");
		Long dataCount = 0L;
		int pageParam = 1;
		String page = request.getParameter("page");
		if (StringUtil.isEmpty(page)) {
			pageParam = 1;
		} else {
			pageParam = Integer.parseInt(page);
		}

		dataCount = systemService.getCountForJdbcParam("select count(1) from bns_article where group_id='" + groupId + "' ORDER BY is_up  desc", null);
		
		//左联用户表查询文章
		String sql = "select  w.*,a.user_id,a.user_name,a.real_name,a.integral,a.experience,a.prestige,a.charm,"
				+ "a.contrbute,a.lunbi from bns_article w "
				+ "LEFT JOIN bns_users_info a on w.lastreply_id = a.user_id where w.group_id='"+groupId+"' ORDER BY w.create_date";
		
		if (StringUtil.isNotEmpty(themeId)) {
			sql += " and w.theme_id='"+themeId+"'";
		}
		
		List<Map<String, Object>>  articleList = systemService.findForJdbc(sql, pageParam, 10); 


		int pageCount = 0;
		if (dataCount.intValue() > 0 && dataCount.intValue() % 10 == 0) {
			pageCount = dataCount.intValue() / 10;
		} else {
			pageCount = dataCount.intValue() / 10 + 1;
		}
		request.setAttribute("pageCount", pageCount);

		ArticleGroupInfoEntity articleGroupInfo = systemService.getEntity(ArticleGroupInfoEntity.class, groupId);

		String themeQuery = "from BnsThemeEntity where groupId = ? ";
		List<BnsThemeEntity> themeList = commonService.findHql(themeQuery, groupId);
		List<Map<String, String>> themeListV = new ArrayList<Map<String, String>>();

		Map<String, String> map = null;
		String themeid = null, themeName = "";
		for (int i = 0; i < themeList.size(); i++) {
			map = new HashMap<String, String>();
			themeid = themeList.get(i).getId();
			themeName = themeList.get(i).getThemeName();
			Long themeNum = systemService.getCountForJdbc("select count(1) from bns_article where theme_id='" + themeid + "' and group_id='" + groupId + "'");
			map.put("id", themeList.get(i).getId());
			map.put("themeName", themeList.get(i).getThemeName());
			themeList.get(i).setThemeName(themeName + " " + themeNum);
			themeListV.add(map);
		}

		request.setAttribute("articleList", articleList); // 文章列表
		request.setAttribute("articleGroupInfo", articleGroupInfo);
		request.setAttribute("themeList", themeList);
		request.setAttribute("themeNum", themeList.size());
		request.setAttribute("themeListV", themeListV);
		// request.setAttribute("mbId", mbId);
		return new ModelAndView("front/groupInfo");
	}

	/**
	 * 添加文章表
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save", method = {RequestMethod.POST,RequestMethod.GET})
	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) {
		String path = "front/login";
		HttpSession session = request.getSession();
		AjaxJson j = new AjaxJson();
		message = "文章表添加成功";
		String mbId = "";
		Object mbIdV = session.getAttribute("mbId");
		if (!StringUtil.isNotEmpty(mbIdV)) {
			return new ModelAndView("front/login");
		}

		// try {
		mbId = mbIdV.toString();
		String areaId = request.getParameter("areaId"); // 区域ID
		String groupId = request.getParameter("groupId");
		String themeId = request.getParameter("themeId");
		String title = request.getParameter("title");
		String content = request.getParameter("content");

		if (areaId.contains("请选择")) {
			j.setMsg("请选择区域");
			j.setSuccess(false);
		}

		if (groupId.contains("请选择")) {
			j.setMsg("请选择版块");
			j.setSuccess(false);
		}

		if (StringUtil.isEmpty(themeId) || themeId.equals("请选择主题")) {
			themeId = "";
		}
		BnsArticleEntity myArticle = new BnsArticleEntity();

		// 保存区域id和版块id到实体中
		myArticle.setAreaId(areaId);
		myArticle.setGroupId(groupId);
		myArticle.setThemeId(themeId);
		myArticle.setTitle(title);
		myArticle.setContent(content);
		// 保存其他参数到实体中
		myArticle.setCreateName(mbId); // 保存发布人
		myArticle.setCreateDate(new Date());

		myArticle.setArticleLabel("artLab_zx"); // 文章标签 保存最新的
		myArticle.setPublisherStates("pbsSta_yfb"); // 发布状态 已发布 pbsSta_yfb，未发布
													// pbsSta_wfb
		myArticle.setCommentsNum(0);
		myArticle.setBrowseNum(0);
		// 保存实体
		commonService.save(myArticle);
		path = "redirect:/frontArticleInfoController.do?articleInfoList&Id=" + myArticle.getId() + "";	//保存帖子 成功重定向到保存的帖子详情
		return new ModelAndView(path);
	}
	

}
